package com.jsmy.softapp.presenter;

import com.jsmy.softapp.MyApp;
import com.jsmy.softapp.bean.LoginBean;
import com.jsmy.softapp.model.LoginModel;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.util.JPushUtils;
import com.jsmy.softapp.util.SPF;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.LoginView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/18.
 * Time: 17:19.
 * To change for file | settings | file and code templates.
 */

public class LoginPresenter extends BasePresenter<LoginView> {
    public LoginModel model = new LoginModel();
    public String user;
    public String psw;

    public void login(String email, String password) {
        if (model != null && getView() != null) {
            getView().showLoading();
            user = email;
            psw = password;
            Map<String, String> map = new HashMap<>();
            map.put("lid", user);
            map.put("pwd", psw);
            model.loadData(API.LOG_IN, map, this);
        }
    }

    @Override
    public void onComplete(String type, String code, String msg, Object object) {
        if (getView() != null) {
            switch (code) {
                case "Y":
                    MyApp.getMyApp().bean = ((LoginBean) object).getData();
                    JPushUtils.loginJMessage(MyApp.getMyApp(), MyApp.getMyApp().bean.getTalkId());
                    SPF.saveString(MyApp.getMyApp(), SPF.SP_ZH, user);
                    SPF.saveString(MyApp.getMyApp(), SPF.SP_MM, psw);
                    SPF.saveString(MyApp.getMyApp(), SPF.SP_ID, MyApp.getMyApp().bean.getId());
                    getView().showLoadingData(type, object);
                    break;
                default:
                    ToastUtil.showShort(MyApp.getMyApp(), msg);
                    break;
            }
            getView().hideLoading();
        }
    }
}
